<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            /* 关 */
            background-color: red;
            /* 只要改了background-color就会触发以下动画 */
            transition: background-color .2s ease;
        }

        .box.on {
            /* 开 */
            background-color: blue;
        }

    </style>
</head>
<body>
    <div class="box"></div>

    <script>

        var box = document.querySelector('.box');

        // 开关思想
        var on = false;
        box.addEventListener('click', function () {
            if (on) {
                box.setAttribute('class', 'box');
            } else {
                box.setAttribute('class', 'box on');
            }
            on = !on;
        });

    </script>
</body>
</html>